package com.wr.hangzhounewlight.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wr.hangzhounewlight.obj.database.OrderEntity;
import com.wr.hangzhounewlight.obj.dto.frontend.RecipeListDTO;
import com.wr.hangzhounewlight.obj.vo.frontend.RecipeListVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author yuanzhizhuo
 * createDate 2025/11/8
 */
public interface OrderMapper extends BaseMapper<OrderEntity> {

    @Insert("""
            insert ignore into `order` (id, device_id, order_no)
            values (#{id}, #{deviceId}, #{orderNo})
            """)
    int insertIgnore(String id, String deviceId, String orderNo);

    @Select("""
            select distinct t2.order_no
            from device as t1
                     left join `order` as t2 on t1.id = t2.device_id
            where t1.workshop = #{workshop}
              and t1.machine = #{machine}
              and t1.screw = #{screw}
              and t2.is_close = 0
            order by t2.insert_time
            limit #{offset}, #{limit};
            """)
    List<String> selectNotClosedOrderNo(String workshop, String machine, String screw, int offset, int limit);

    @Select("""
            select t1.workshop,
                   t1.machine,
                   t1.screw,
                   t2.id as order_id,
                   t2.order_no,
                   t2.recipe_is_check,
                   t2.is_close
            from device as t1
                     left join `order` as t2 on t1.id = t2.device_id
            where t1.workshop = #{workshop}
              and t1.machine = #{machine}
              and t1.screw = #{screw}
              and t2.order_no = #{orderNo}
              and t2.recipe_is_check = ${recipeIsCheck}
              and t2.is_close = ${isClose}
              and t2.insert_time between #{startDateTime} and #{endDateTime};
            """)
    List<RecipeListVO> selectOrderVO(RecipeListDTO dto);

}
